<?php
    date_default_timezone_set('Europe/Kiev');

        if(file_exists('config.php'))       require_once('config.php');   # load configuration file
	elseif(file_exists('./sys/config.php')) require_once('./sys/config.php');
	else exit('<b>Error(index.php)</b> config.php not found!');



	require_once(PATCH_KERNEL."init_base.php");   # load base initilization of portal
	
	
	if(empty($_SESSION['lang'])) $_SESSION['lang']='ru';

	if(isset($_GET['load_js']) && isset($_GET['m'])) {

		if( is_file("./modules/{$_GET['m']}/languages/{$_SESSION['lang']}/{$_GET['load_js']}.js.php") ) {
			require_once("./modules/{$_GET['m']}/languages/{$_SESSION['lang']}/{$_GET['load_js']}.js.php");
		}
		if( is_file("./modules/{$_GET['m']}/js/{$_GET['load_js']}.js") ) {
			require_once("./modules/{$_GET['m']}/js/{$_GET['load_js']}.js");
		} else echo "alert('Filed to open JS file: {$_GET['load_js']}, using module: {$_GET['m']}');";
		exit();
	}

	if(isset($_SESSION['lang'])) define("DEFAULT_LANGUAGE", $_SESSION['lang']);
	else define("DEFAULT_LANGUAGE", "en");

	$timeBegin = time();
	# Parse this URL to CMS standart use.
		function parse_url_to_cms_standart($path) {
			if (strpos($path,"?") !== false) {
				$params=substr($path, strpos($path,"?")+1);
				$path=substr($path, 0, strpos($path,"?"));
			}
			if (strpos($path, "/") === 0) {
				$path=substr($path, 1, strlen($path));
			}

			$args    = explode('/', $path);
			$args[0] = explode('-', $args[0]);

			 $return      = $args[0];
			 $return['m'] = &$return[0];
			 $return['p'] = &$return[1];
			 unset($args[0]);

			foreach($args as $args1 => $args2) {
				if(isset($i)) {
					$return[$i] = $args2;
					unset($i);
				} else {
					$return[$args2] = true;
					$i = $args2;
				}
			}

			return $return;
		}

		$_GET = array_merge($_GET, parse_url_to_cms_standart(
			(empty($_SERVER['PATH_INFO'])? $_SERVER['REQUEST_URI'] : $_SERVER['PATH_INFO'])
		));
	


	require_once(PATCH_KERNEL."init.php");		# load init

		 //print_r($_SESSION);
		 //print_r($init);
	$timeEnd = time();
/*
	$fo = fopen("./sys/Logs/access.txt", "a");
		fputs($fo, $_SERVER['REQUEST_URI']." | ".$timeBegin." | ".($timeEnd-$timeBegin)." | ".serialize(array("request"=>$_REQUEST, "post"=>$_POST))."\r\n");
	fclose($fo);
*/
	//print_r($init);
	exit();